﻿using System;
using System.Collections.Generic;
using System.Web.Security;
using MySql.Data.MySqlClient;

namespace Possan.MySQL.Providers
{
	class MembershipCache
	{
		private string _name;
		private string _connStr;
		private string _usertablename;
		private static object _lockobject = new object();
		private static Dictionary<string, MembershipUser> _cachedata = new Dictionary<string, MembershipUser>();
		private string _membershiptablename;

		public MembershipCache( string connStr, string name )
		{
			_connStr = connStr;
			_name = name;
			_usertablename = "aspnet_users";
			_membershiptablename = "aspnet_membership";
		}

		public MembershipUser GetById(string userid)
		{
			MembershipUser ret = null;

			lock (_lockobject)
			{
				if (_cachedata.ContainsKey(userid))
					ret = _cachedata[userid];
			} 

			if (ret != null)
				return ret;
				
			using (var conn = new MySqlConnection(_connStr))
			{
				conn.Open();
				using (var cmd = conn.CreateCommand())
				{
					cmd.CommandText = @"select u.*, m.*
						from " + _usertablename + @" u
						inner join " + _membershiptablename + @" m on m.UserId = u.UserId
						where u.userid = @a";

					cmd.Parameters.Add(new MySqlParameter("@a", userid));
					using (MySqlDataReader rdr = cmd.ExecuteReader())
					{
						if (rdr.Read())
						{
							ret = _innerGetUser(rdr);
						}
						rdr.Close();
					}
					cmd.Dispose();
				}
				conn.Close();
			}

			if (ret != null)
				lock (_lockobject)
				{
					if (_cachedata.ContainsKey(userid))
						_cachedata[userid] = ret;
					else
						_cachedata.Add(userid, ret);
				}

			return ret;
		}

		public void Flush(string userid)
		{

			lock (_lockobject)
			{
				if (_cachedata.ContainsKey(userid))
					_cachedata.Remove(userid);
			}

		}

		public void FlushAll()
		{

			lock (_lockobject)
			{
				_cachedata.Clear();
			}

		}
		
		private MembershipUser _innerGetUser(MySqlDataReader reader)
		{
			string userid = (string)reader["UserId"];
			string username = (string)reader["UserName"];
			string email = (string)reader["Email"];
			int lockout = 0;
			int.TryParse(reader["IsLockedOut"].ToString(), out lockout);
			int approved = 0;
			int.TryParse(reader["IsApproved"].ToString(), out approved);
			string passq = "";// (string)reader["PasswordQuestion"];
			string comment = ""; //  (string)reader["Comment"];
			DateTime creationdate = DateTime.Now;// (string)reader["PasswordQuestion"];
			DateTime lastlogindate = DateTime.Now;
			DateTime lastactivitydate = DateTime.Now;
			DateTime lastpasswordchangedate = DateTime.Now;
			DateTime lastlockoutdate = DateTime.Now;
			// string passq = (string)reader["PasswordQuestion"];
			// string passq = (string)reader["PasswordQuestion"];
			// string passq = (string)reader["PasswordQuestion"];
			// string passq = (string)reader["PasswordQuestion"];

			return new MembershipUser(
				_name,
				username,
				userid,
				email,
				passq,
				comment,
				(approved == 1),
				(lockout == 1),
				creationdate,
				lastlogindate,
				lastactivitydate,
				lastpasswordchangedate,
				lastlockoutdate);

		}



	}
}
